﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Ge();
        }

        private void Ge()
        {
            Image img = new Image();
            Image img2 = new Image();

            img.Source = CardGenerator.Gen(CardType.Heart, CardNumberType.Two);
            //img2.Source = CardGenerator.Gen(CardType.Heart, CardNumberType.A);
            img.Stretch = Stretch.Fill;
            DockPanel dp = new DockPanel();
            dp.Background = Brushes.Black;

            dp.Margin = new Thickness(-60,0,0,0);

            img.Width = 120;
            img.Height = 300;

            dp.Height = 150;
            dp.Width = 120;

            

            dp.Opacity = 0.5;
            sp.Children.Add(img);
            sp.Children.Add(dp);

            
        }

        private void movedown(object sender, RoutedEventArgs args)
        {
            MessageBox.Show("left");
            var dp = sender as Image;
            dp.Margin = new Thickness(0,100,0,0);
        }

        private void right(object sender, RoutedEventArgs args)
        {
            var img = sender as Image;
            img.Margin = new Thickness(0,-100,0,0);
        }


    }



    public enum CardType
    {
        Heart,
        Spade,
        Club,
        Diamond
    }

     [Serializable]
    public class Card
    {
        private CardType cardType;
        private CardNumberType cardNumberType;

        public CardType CardType
        {
            set { cardType = value; }
            get { return cardType; }
        }

        public CardNumberType CardNumberType
        {
            set { cardNumberType = value; }
            get { return cardNumberType; }
        }

        public Card(CardType ct, CardNumberType cnt)
        {
            this.cardType = ct;
            this.cardNumberType = cnt;
        }

        public override int GetHashCode()
        {
            return ((int)cardType) * 100 + ((int)cardNumberType);
        }

        public override bool Equals(object obj)
        {
            Card card = obj as Card;
            return cardType == card.cardType  && cardNumberType == card.cardNumberType ;
        }
    }
    public enum CardNumberType
    {
        A,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }
    public class CardGenerator
    {
        private static BitmapImage bitmapImage = new BitmapImage(new Uri(@"puke.jpg", UriKind.Relative));
        private static BitmapSource Generate(CardType cardType, CardNumberType cardNumType)
        {

            BitmapSource bs = new BitmapImage(new Uri("puke.jpg", UriKind.Relative));
            int height = 165;
            int width = 110;
            int marginTop = 14;
            int marginLeft = 14;

            Int32Rect cut = new Int32Rect(marginLeft, marginTop, width, height);
            
            

            switch (cardType)
            {
                case CardType.Diamond:
                    switch (cardNumType)
                    {
                        case CardNumberType.A:
                            cut = new Int32Rect(marginLeft, marginTop, width, height);
                            break;

                        case CardNumberType.Two:
                            cut = new Int32Rect(marginLeft + width, marginTop, width, height);
                            break;

                        case CardNumberType.Three:

                            cut = new Int32Rect(marginLeft +2* width, marginTop, width, height);
                            break;

                        case CardNumberType.Four:
                            cut = new Int32Rect(marginLeft + 3*width, marginTop, width, height);
                            break;

                        case CardNumberType.Five:
                            cut = new Int32Rect(marginLeft + 4*width, marginTop, width, height);
                            break;

                        case CardNumberType.Six:
                            cut = new Int32Rect(marginLeft + 5*width, marginTop, width, height);
                            break;

                        case CardNumberType.Seven:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop, width, height);
                            break;

                        case CardNumberType.Eight:
                            cut = new Int32Rect(marginLeft +7* width, marginTop, width, height);
                            break;

                        case CardNumberType.Nine:
                            cut = new Int32Rect(marginLeft + 8*width, marginTop, width, height);

                            break;

                        case CardNumberType.Ten:
                            cut = new Int32Rect(marginLeft, marginTop+height, width, height);
                            break;

                        case CardNumberType.Jack:
                            cut = new Int32Rect(marginLeft + width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Queen:
                            cut = new Int32Rect(marginLeft + 2*width, marginTop + height, width, height);
                            break;

                        case CardNumberType.King:
                            cut = new Int32Rect(marginLeft + 3*width, marginTop + height, width, height);
                            break;
                        default:
                            break;

                    }
                    break;
                case CardType.Heart:
                    switch (cardNumType)
                    {
                        case CardNumberType.A:
                            cut = new Int32Rect(marginLeft + 4*width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Two:
                            cut = new Int32Rect(marginLeft +5* width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Three:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Four:
                            cut = new Int32Rect(marginLeft + 7*width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Five:
                            cut = new Int32Rect(marginLeft + 8*width, marginTop + height, width, height);
                            break;

                        case CardNumberType.Six:
                            cut = new Int32Rect(marginLeft, marginTop + 2*height, width, height);
                            break;

                        case CardNumberType.Seven:
                            cut = new Int32Rect(marginLeft + width, marginTop + 2*height, width, height);
                            break;

                        case CardNumberType.Eight:
                            cut = new Int32Rect(marginLeft +2* width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.Nine:
                            cut = new Int32Rect(marginLeft +3* width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.Ten:
                            cut = new Int32Rect(marginLeft + 4*width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.Jack:
                            cut = new Int32Rect(marginLeft + 5*width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.Queen:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.King:
                            cut = new Int32Rect(marginLeft + 7*width, marginTop + 2 * height, width, height);
                            break;

                        default:

                            break;
                    }

                    break;
                case CardType.Club:

                    switch (cardNumType)
                    {
                        case CardNumberType.A:
                            cut = new Int32Rect(marginLeft + 8*width, marginTop + 2 * height, width, height);
                            break;

                        case CardNumberType.Two:
                            cut = new Int32Rect(marginLeft , marginTop + 3 * height, width, height);
                            break;

                        case CardNumberType.Three:
                            cut = new Int32Rect(marginLeft + width, marginTop + 3 * height, width, height);
                            break;

                        case CardNumberType.Four:
                            cut = new Int32Rect(marginLeft + 2*width, marginTop + 3 * height, width, height);
                            break;

                        case CardNumberType.Five:
                            cut = new Int32Rect(marginLeft + 3*width, marginTop + 3 * height, width, height);
                            break;

                        case CardNumberType.Six:
                            cut = new Int32Rect(marginLeft + 4*width, marginTop + 3 * height, width, height);

                            break;

                        case CardNumberType.Seven:
                            cut = new Int32Rect(marginLeft + 5*width, marginTop + 3 * height, width, height);

                            break;

                        case CardNumberType.Eight:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop + 3 * height, width, height);
                            break;

                        case CardNumberType.Nine:
                            cut = new Int32Rect(marginLeft + 7*width, marginTop + 3 * height, width, height);

                            break;

                        case CardNumberType.Ten:
                            cut = new Int32Rect(marginLeft + 8*width, marginTop + 3 * height, width, height);;

                            break;

                        case CardNumberType.Jack:
                            cut = new Int32Rect(marginLeft , marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Queen:
                            cut = new Int32Rect(marginLeft + width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.King:
                            cut = new Int32Rect(marginLeft + 2*width, marginTop + 4 * height, width, height);
                            break;

                        default:

                            break;
                    }
                    break;
                case CardType.Spade:
                    switch (cardNumType)
                    {
                        case CardNumberType.A:
                            cut = new Int32Rect(marginLeft + 3*width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Two:
                            cut = new Int32Rect(marginLeft + 4*width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Three:
                            cut = new Int32Rect(marginLeft + 5*width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Four:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Five:
                            cut = new Int32Rect(marginLeft + 7*width, marginTop + 4 * height, width, height);
                            break;

                        case CardNumberType.Six:
                            cut = new Int32Rect(marginLeft + 8*width, marginTop + 4 * height, width, height);

                            break;

                        case CardNumberType.Seven:
                            cut = new Int32Rect(marginLeft , marginTop + 5 * height, width, height);
                            break;

                        case CardNumberType.Eight:
                            cut = new Int32Rect(marginLeft + width, marginTop + 5 * height, width, height);
                            break;

                        case CardNumberType.Nine:
                            cut = new Int32Rect(marginLeft + 2*width, marginTop + 5 * height, width, height);

                            break;

                        case CardNumberType.Ten:
                            cut = new Int32Rect(marginLeft + 3*width, marginTop + 5 * height, width, height);

                            break;

                        case CardNumberType.Jack:
                            cut = new Int32Rect(marginLeft +4* width, marginTop + 5 * height, width, height);

                            break;

                        case CardNumberType.Queen:
                            cut = new Int32Rect(marginLeft + 5*width, marginTop + 5 * height, width, height);
                            break;

                        case CardNumberType.King:
                            cut = new Int32Rect(marginLeft + 6*width, marginTop + 5 * height, width, height);
                            break;

                        default:

                            break;
                    }

                    break;

                default:
                    break;

            }

            int stride = cut.Width * bs.Format.BitsPerPixel / 8;
            byte[] bytes = new byte[stride * cut.Height];
            bs.CopyPixels(cut, bytes, stride, 0);

            var src = BitmapSource.Create(width, height, 0, 0, PixelFormats.Bgr32, null, bytes, stride);
            
            return src;
        }

        public static BitmapSource Gen(CardType type, CardNumberType numtype)
        {
            return bitmapSources[new Card(type, numtype)];
        }

        private static Dictionary<Card, BitmapSource> bitmapSources;

        static CardGenerator()
        {
            bitmapSources = new Dictionary<Card, BitmapSource>();
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.A), Generate(CardType.Heart, CardNumberType.A));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Two), Generate(CardType.Heart, CardNumberType.Two));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Three), Generate(CardType.Heart, CardNumberType.Three));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Four), Generate(CardType.Heart, CardNumberType.Four));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Five), Generate(CardType.Heart, CardNumberType.Five));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Six), Generate(CardType.Heart, CardNumberType.Six));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Seven), Generate(CardType.Heart, CardNumberType.Seven));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Eight), Generate(CardType.Heart, CardNumberType.Eight));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Nine), Generate(CardType.Heart, CardNumberType.Nine));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Ten), Generate(CardType.Heart, CardNumberType.Ten));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Jack), Generate(CardType.Heart, CardNumberType.Jack));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.Queen), Generate(CardType.Heart, CardNumberType.Queen));
            bitmapSources.Add(new Card(CardType.Heart, CardNumberType.King), Generate(CardType.Heart, CardNumberType.King));

            bitmapSources.Add(new Card(CardType.Club, CardNumberType.A), Generate(CardType.Club, CardNumberType.A));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Two), Generate(CardType.Club, CardNumberType.Two));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Three), Generate(CardType.Club, CardNumberType.Three));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Four), Generate(CardType.Club, CardNumberType.Four));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Five), Generate(CardType.Club, CardNumberType.Five));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Six), Generate(CardType.Club, CardNumberType.Six));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Seven), Generate(CardType.Club, CardNumberType.Seven));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Eight), Generate(CardType.Club, CardNumberType.Eight));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Nine), Generate(CardType.Club, CardNumberType.Nine));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Ten), Generate(CardType.Club, CardNumberType.Ten));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Jack), Generate(CardType.Club, CardNumberType.Jack));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.Queen), Generate(CardType.Club, CardNumberType.Queen));
            bitmapSources.Add(new Card(CardType.Club, CardNumberType.King), Generate(CardType.Club, CardNumberType.King));

            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.A), Generate(CardType.Spade, CardNumberType.A));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Two), Generate(CardType.Spade, CardNumberType.Two));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Three), Generate(CardType.Spade, CardNumberType.Three));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Four), Generate(CardType.Spade, CardNumberType.Four));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Five), Generate(CardType.Spade, CardNumberType.Five));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Six), Generate(CardType.Spade, CardNumberType.Six));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Seven), Generate(CardType.Spade, CardNumberType.Seven));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Eight), Generate(CardType.Spade, CardNumberType.Eight));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Nine), Generate(CardType.Spade, CardNumberType.Nine));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Ten), Generate(CardType.Spade, CardNumberType.Ten));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Jack), Generate(CardType.Spade, CardNumberType.Jack));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.Queen), Generate(CardType.Spade, CardNumberType.Queen));
            bitmapSources.Add(new Card(CardType.Spade, CardNumberType.King), Generate(CardType.Spade, CardNumberType.King));

            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.A), Generate(CardType.Diamond, CardNumberType.A));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Two), Generate(CardType.Diamond, CardNumberType.Two));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Three), Generate(CardType.Diamond, CardNumberType.Three));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Four), Generate(CardType.Diamond, CardNumberType.Four));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Five), Generate(CardType.Diamond, CardNumberType.Five));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Six), Generate(CardType.Diamond, CardNumberType.Six));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Seven), Generate(CardType.Diamond, CardNumberType.Seven));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Eight), Generate(CardType.Diamond, CardNumberType.Eight));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Nine), Generate(CardType.Diamond, CardNumberType.Nine));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Ten), Generate(CardType.Diamond, CardNumberType.Ten));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Jack), Generate(CardType.Diamond, CardNumberType.Jack));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.Queen), Generate(CardType.Diamond, CardNumberType.Queen));
            bitmapSources.Add(new Card(CardType.Diamond, CardNumberType.King), Generate(CardType.Diamond, CardNumberType.King));
        }
    }
}
